import Element from 'element-ui'
import "element-ui/lib/theme-chalk/index.css"
import axios from 'axios'
import router from './router'
import store from './store'


axios.defaults.baseURL="http://localhost:8080/myblog";

//前置拦截
axios.interceptors.request.use(config => {
    return config;
});

//后置拦截
axios.interceptors.response.use(
    //成功的请求
    function (response)  {
        let res = response.data;
        console.log("========================");
        console.log(res);
        console.log("========================");

        if (res.code === 200) {
            return response;
        }else{
            Element.Message.error(res.msg, { duration: 2 * 1000 });
            return Promise.reject(res.msg);//目的是拦截之后阻止程序继续运行下去
        }
    },
    //错误的请求：状态编码不是200的
    error => {
        let res_err = error.response.data;
        if(res_err){
            error.Message = res_err.msg;
        }
        
        if(error.response.status === 401){
            store.commit("REMOVE_INFO");
            router.push("/login");
        }
        
        Element.Message.error(error.Message, { duration: 2 * 1000 });
        return Promise.reject(error.Message);
        
    }
)